package com.sport.interceptor;

import com.sport.bean.Admin;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 *
 * @Author wxq
 * @Date 2021/5/13
 **/
public class MainInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        String header = request.getHeader("X-Requested-With");
        StringBuffer url = request.getRequestURL();
        System.out.println(url);
        if (header != null && "XMLHttpRequest".equals(header)) {
            if (admin != null) {
                return true;//放行
            } else {
                //  2021/5/15 这边的重定向路径不能回到login.html
                response.sendRedirect("/sport/");
                return false;
            }

        }
        //普通的请求
        else {
            if (admin != null) {
                return true;//放行
            } else {

                // 2021/5/15 这边的重定向路径不能回到login.html
                response.sendRedirect("/sport/");
                return false;
            }

        }


    }
}
